﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.Client;

namespace ECare.Win.controls
{
    public partial class ucPlansWorking : DevExpress.XtraEditors.XtraUserControl
    {
        public ucPlansWorking()
        {
            InitializeComponent();

            this.Load += ucPlansWorking_Load;
        }

        void ucPlansWorking_Load(object sender, EventArgs e)
        {
            this.tileControl1.Groups.Clear();
            this.LoadPlans();
        }

        private void LoadPlans()
        {
            var source = PlanHelper.GetPlans("1=1 ", true);

            foreach (var plan in source)
            {
                TileGroup newgroup = null;
                if (this.tileControl1.GetTileGroupByName(plan.plan_date.ToString("yyyyMMdd")) == null)
                {
                    newgroup = new TileGroup { Name = plan.plan_date.ToString("yyyyMMdd") };
                    newgroup.Text = plan.plan_date.ToString("yyyy-MM-dd"); this.tileControl1.Groups.Add(newgroup);

                }
                else
                {
                    newgroup = this.tileControl1.GetTileGroupByName(plan.plan_date.ToString("yyyyMMdd"));
                } var newitem = new TileItem
                 {
                     ItemSize = TileItemSize.Wide,
                     Text = FormulaHelper.GetFormulaInfoById(plan.formula_id),
                     Tag = plan
                 };

                // newitem = plan.plan_date.ToString("yyyyMMdd");
                var info = new DevExpress.XtraEditors.TileItemElement();
                var detailes = PlanHelper.GetPlanDetails(plan);

                info.Text = string.Format("总计 {0}, 未完成 {1}", plan.plan_times,
                    detailes.Count(a => a.plan_detail_status == PlanStatus.pending));

                newitem.Elements.Add(info);

                int total = plan.plan_times;
                int pending = detailes.Count(a => a.plan_detail_status == PlanStatus.pending);

                if (total == pending)
                {
                    //not started.

                    newitem.Appearance.BackColor = System.Drawing.Color.FromArgb(((int) (((byte) (84)))),
                        ((int) (((byte) (175)))), ((int) (((byte) (14)))));
                    newitem.Appearance.BackColor2 = System.Drawing.Color.FromArgb(((int) (((byte) (104)))),
                        ((int) (((byte) (194)))), ((int) (((byte) (16)))));
                    newitem.Appearance.BorderColor = System.Drawing.Color.FromArgb(((int) (((byte) (127)))),
                        ((int) (((byte) (203)))), ((int) (((byte) (52)))));
                }
                else if (pending/total > 0.8)
                {
                    newitem.Appearance.BackColor = System.Drawing.Color.FromArgb(((int) (((byte) (212)))),
                        ((int) (((byte) (175)))), ((int) (((byte) (0)))));
                    newitem.Appearance.BackColor2 = System.Drawing.Color.FromArgb(((int) (((byte) (224)))),
                        ((int) (((byte) (200)))), ((int) (((byte) (26)))));
                    newitem.Appearance.BorderColor = System.Drawing.Color.FromArgb(((int) (((byte) (218)))),
                        ((int) (((byte) (187)))), ((int) (((byte) (38)))));
                }
                else
                {
                    newitem.AppearanceItem.Normal.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(114)))), ((int)(((byte)(198)))));
                }






                var infodate = new DevExpress.XtraEditors.TileItemElement { Text = plan.plan_date.ToShortDateString() };

                newitem.Elements.Add(infodate);

          


                newitem.ItemClick += newitem_ItemClick;

                newgroup.Items.Add(newitem);
            }


        }

        void newitem_ItemClick(object sender, TileItemEventArgs e)
        {
            //throw new NotImplementedException();
            var currentplan = e.Item.Tag as PlanInfo;
            var frmwizard = new frmWizard {currentPlan = currentplan};
            frmwizard.ShowDialog();

        }

    }
}
